<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar title="编辑资料" left-arrow @click-left="$router.back()" fixed />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image round class="avatar" :src="BasicUserInformation.photo" @click="imgClickFn"/>
          <!-- file 选择框 -->
        <input
               type="file"
               ref="iptFile"
               v-show="false"
               accept="image/*"
               @change="onFileChange"
               />
        </template>
      </van-cell>
      <van-cell title="名称" is-link  :value="BasicUserInformation.name" @click="nameClickFn"/>
      <van-cell title="生日" is-link :value="BasicUserInformation.birthday" @click="birthdayClickFn" />
    </van-cell-group>
    <!-- 修改名称时弹窗 -->
     <van-dialog v-model="show" title="标题" show-cancel-button :before-close='beforeCloseFn'>
           <input type="text" v-focus v-model="inputUserName">
     </van-dialog>
     <!-- 时间选择器 -->
    <!-- 组件可以自己搭配使用 -->
    <van-popup round v-model="dateTimePickerShow"  position="bottom" :style="{ height: '50%' }">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="dateCancelFn"
        @confirm="dateConfirmFn"
      />
    </van-popup>
  </div>
</template>

<script>
import { userProfileAPI, updateUserPhotoAPI, updateUserProfileAPI } from '@/api'
import Notify from '@/ui/Notify'
import { formatDate } from '@/utils/date.js'
import { mapMutations } from 'vuex'
export default {
  name: 'UserEdit',
  data () {
    return {
      BasicUserInformation: {},
      show: false,
      inputUserName: '',
      minDate: new Date(1920, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(),
      dateTimePickerShow: false // 是否展示时间选择器
    }
  },
  async created () {
    const res = await userProfileAPI()
    this.BasicUserInformation = res.data.data
    console.log('编辑页面', res)
  },
  methods: {
    // 文件选择改变 -事件
    ...mapMutations(['SET_USRPHOTO']),
    async onFileChange (e) {
    //   console.log(e.target.files[0]) // （用户选中文件对象）数组其实是一个特殊的对象
      if (e.target.files.length === 0) return // 解决用户更改头像点击取消选择
      const theFd = new FormData()
      theFd.append('photo', e.target.files[0]) // 请求体里面加入一对参数名和值
      const res = await updateUserPhotoAPI(theFd)
      this.BasicUserInformation.photo = res.data.data.photo
      this.SET_USRPHOTO(res.data.data.photo) // 更新成功后同步到vuex中
      console.log(res)
    },
    // 图片-点击事件
    imgClickFn () {
      this.$refs.iptFile.click()
    },
    // 名称点击事件
    nameClickFn () {
      this.show = true
      this.inputUserName = this.BasicUserInformation.name
    },
    // 名字验证以及修改
    async beforeCloseFn (action, done) {
      if (action === 'confirm') {
        const reg = /^[a-zA-Z0-9\u4e00-\u9fa5]{1,7}$/
        if (reg.test(this.inputUserName) === true) {
          // 通过了校验
          await updateUserProfileAPI({
            name: this.inputUserName
          })
          this.BasicUserInformation.name = this.inputUserName // 更新成功 - 回显到页面上
          done()
        }
      } else {
        Notify({
          type: 'warning',
          message: '请输入1-7位中英文组合'
        })
        done(false)
      }
    },
    // 生日点击事件
    birthdayClickFn () {
      this.dateTimePickerShow = true
      this.currentDate = new Date(this.BasicUserInformation.birthday)
    },
    // 取消日期选择器
    dateCancelFn () {
      this.dateTimePickerShow = false
    },
    // 日期选择器确认
    async dateConfirmFn () {
      // 日期选择器组件里currentDate是日期对象, 而后端要的是年-月-日字符串
      await updateUserProfileAPI({
        birthday: formatDate(this.currentDate)
      })
      this.dateTimePickerShow = false
      this.BasicUserInformation.birthday = formatDate(this.currentDate) // 单元格同步
    }
  }

}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
/deep/.van-dialog{
    text-align: center;
    input{
        border: none;
        outline: none;
        text-align: center;
    }
}
</style>
